package com.dxf.sysadmin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dxf.common.utils.ResultJson;
import com.dxf.sysadmin.entity.Clazz;
import com.dxf.sysadmin.entity.Course;
import com.dxf.sysadmin.entity.CourseClazzTeacher;
import com.dxf.sysadmin.entity.Emp;
import com.dxf.sysadmin.entity.qo.MyPage;
import com.dxf.sysadmin.entity.vo.CourseClazzTeacherVo;
import com.dxf.sysadmin.mapper.CourseClazzTeacherMapper;
import com.dxf.sysadmin.service.ClazzService;
import com.dxf.sysadmin.service.CourseClazzTeacherService;
import com.dxf.sysadmin.service.CourseService;
import com.dxf.sysadmin.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 课表 服务实现类
 */
@Service
public class CourseClazzTeacherServiceImpl extends ServiceImpl<CourseClazzTeacherMapper, CourseClazzTeacher> implements CourseClazzTeacherService {

    @Autowired
    private CourseService courseService;
    @Autowired
    private ClazzService clazzService;
    @Autowired
    private EmpService empService;

    /**
     *课表列表分页查询
     */
    @Override
    public ResultJson coursePage(MyPage qry) {
        Page<CourseClazzTeacherVo> page = new Page<>(qry.getPage(), qry.getLimit());

        Map<String, Object> map = new HashMap<>();
        Page<CourseClazzTeacherVo> courseVoPage = baseMapper.selectCctPage(page);

        map.put("rows",courseVoPage.getRecords());
        map.put("total",courseVoPage.getTotal());
        return ResultJson.ok().data(map);
    }

    /**
     *下拉选项数据
     */
    @Override
    public ResultJson selectData() {
        List<Course> courses = courseService.list();
        List<Clazz> clazzes = clazzService.list();
        List<Emp> emps = empService.list(new QueryWrapper<Emp>().lambda().eq(Emp::getDuty,2));
        return ResultJson.ok().data("courses",courses).
                data("clazzes",clazzes).data("emps",emps);
    }

    /**
     *新增或者更新课表
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultJson saveOrUpdateCct(CourseClazzTeacher cct) {
        saveOrUpdate(cct);
        return ResultJson.ok();
    }

    /**
     *更具id列表删除课表
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultJson deleteCct(List<Long> list) {
        removeByIds(list);
        return ResultJson.ok();
    }
}
